<template>
  <div class="huowei-list-container">
    <!-- 查询表单 -->
    <el-form :model="queryForm" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="盘点号" prop="inventoryId">
        <el-input
          v-model="queryForm.inventoryId"
          placeholder="请输入盘点号（必填）"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检查时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 货位汇总表格 -->
    <el-table v-loading="loading" :data="huoweiList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="货位ID" align="center" prop="huoweiId" />
      <el-table-column label="变更商品数" align="center" prop="drugCount">
        <template slot-scope="scope">
          <el-tag type="info">{{ scope.row.drugCount }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="变更记录总数" align="center" prop="totalChangeCount">
        <template slot-scope="scope">
          <el-tag type="primary">{{ scope.row.totalChangeCount }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="盘点状态统计" align="center" min-width="400">
        <template slot-scope="scope">
          <div class="change-type-stats">
            <el-tag v-if="scope.row.normalCount > 0" type="success" size="mini" class="stat-tag">
              正常盘点: {{ scope.row.normalCount }}
            </el-tag>
            <el-tag v-if="scope.row.unInventoriedCount > 0" type="warning" size="mini" class="stat-tag">
              未盘点: {{ scope.row.unInventoriedCount }}
            </el-tag>
            <el-tag v-if="scope.row.wrongDeliveryCount > 0" type="danger" size="mini" class="stat-tag">
              异常: {{ scope.row.wrongDeliveryCount }}
            </el-tag>
          </div>
          <div class="change-type-stats" style="margin-top: 5px;">
            <el-tag v-if="scope.row.quantityChangeCount > 0" type="info" size="mini" class="stat-tag">
              数量更改: {{ scope.row.quantityChangeCount }}
            </el-tag>
            <el-tag v-if="scope.row.locationChangeCount > 0" type="info" size="mini" class="stat-tag">
              货位调换: {{ scope.row.locationChangeCount }}
            </el-tag>
            <el-tag v-if="scope.row.expiryChangeCount > 0" type="info" size="mini" class="stat-tag">
              效期更改: {{ scope.row.expiryChangeCount }}
            </el-tag>
            <el-tag v-if="scope.row.batchChangeCount > 0" type="info" size="mini" class="stat-tag">
              批号更改: {{ scope.row.batchChangeCount }}
            </el-tag>
            <el-tag v-if="scope.row.otherChangeCount > 0" type="info" size="mini" class="stat-tag">
              其它信息: {{ scope.row.otherChangeCount }}
            </el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleViewDrugs(scope.row)"
          >查看商品</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryForm.pageNum"
      :limit.sync="queryForm.pageSize"
      :page-sizes="[50, 100, 200, 500]"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { analyzeByInventoryId } from '@/api/report/inventoryChange'
import Pagination from '@/components/Pagination'
import RightToolbar from '@/components/RightToolbar'

export default {
  name: 'HuoweiList',
  components: {
    Pagination,
    RightToolbar
  },
  props: {
    queryParams: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 货位汇总表格数据
      huoweiList: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryForm: {
        pageNum: 1,
        pageSize: 50,
        inventoryId: '',
        startTime: '',
        endTime: ''
      }
    }
  },
  created() {
    this.initQueryParams()
    this.getList()
  },
  methods: {
    /** 初始化查询参数 */
    initQueryParams() {
      if (this.queryParams.inventoryId) {
        this.queryForm.inventoryId = this.queryParams.inventoryId
      }
      if (this.queryParams.startTime && this.queryParams.endTime) {
        this.dateRange = [this.queryParams.startTime, this.queryParams.endTime]
        this.queryForm.startTime = this.queryParams.startTime
        this.queryForm.endTime = this.queryParams.endTime
      }
    },

    /** 查询货位汇总列表 */
    getList() {
      // 验证盘点号是否填写
      if (!this.queryForm.inventoryId) {
        this.$modal.msgError('请输入盘点号')
        this.loading = false
        return
      }

      this.loading = true
      if (this.dateRange && this.dateRange.length === 2) {
        this.queryForm.startTime = this.dateRange[0]
        this.queryForm.endTime = this.dateRange[1]
      } else {
        this.queryForm.startTime = ''
        this.queryForm.endTime = ''
      }

      // 使用新的分析接口
      analyzeByInventoryId(this.queryForm.inventoryId, this.queryForm).then(response => {
        this.huoweiList = response.rows
        this.total = response.total
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryForm.pageNum = 1
      this.getList()
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.$refs.queryForm.resetFields()
      this.handleQuery()
    },

    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.huoweiId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    /** 导出按钮操作 */
    handleExport() {
      this.$download.excel(this.queryForm, `货位变更汇总_${new Date().getTime()}.xlsx`)
    },

    /** 查看货位下的商品 */
    handleViewDrugs(row) {
      this.$emit('view-drugs', row.huoweiId)
    }
  }
}
</script>

<style scoped>
.change-type-stats {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  justify-content: center;
}

.stat-tag {
  margin: 2px;
}
</style>
